package org.ccahouse.ccacommons.utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * 大数字类型工具类
 * 
 * @author kevin
 *
 */
public final class NumberUtils {
	
	//默认的格式化表达式：默认为小数点后一位
	public static final String FORMATE_0_0 = "0.0";

	//小数点后两位
	public static final String FORMATE_0_00 = "0.00";

	//小数点四位
	public static final String FORMATE_0_0000 = "0.0000";

	/**
	 * double类型:小数点后保留一位
	 *
	 * @param number
	 * @return
	 */
	public static String formateDoubleDef(double number) {
		NumberFormat numberFormate = new DecimalFormat(FORMATE_0_0);
		return numberFormate.format(number);
	}

	/**
	 * 小数点后两位:
	 * @param number
	 * @return
	 */
	public static String formateDouble2(double number) {
		NumberFormat numberFormate = new DecimalFormat(FORMATE_0_00);
		return numberFormate.format(number);
	}
	/**
	 * 格式化数字
	 *
	 * @param number
	 * @param formate
	 * @return
	 */
	public static String formateDouble(double number, String formate) {
		if (StringUtils.isEmpty(formate)) {
			return formateDoubleDef(number);
		}
		NumberFormat numberFormate = new DecimalFormat(formate);
		return numberFormate.format(number);
	}


	/**
	 * 大整形转变为基本数据类型-int
	 * 
	 * @param bigInteger
	 * @return
	 */
	public static int BigIntegerToInt(BigInteger bigInteger) {
		return bigInteger == null ? 0 : bigInteger.intValue();
	}

	/**
	 * 大整形转变为基本数据类型-long
	 * 
	 * @param bigInteger
	 * @return
	 */
	public static long BigIntegerToLong(BigInteger bigInteger) {
		return bigInteger == null ? 0 : bigInteger.longValue();
	}

	/**
	 * 两个大数字相除，
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigDecimal  divide(BigDecimal one, BigDecimal two) {
		if(one == null || two == null || two.intValue() == 0){
			return BigDecimal.ZERO;
		}
		return one.divide(two);
	}
	
	/**
	 * 两个大整型数字相除，获取double类型：one / two
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static double divide(BigInteger one, BigInteger two) {
		if(one == null || two == null || two.intValue() == 0){
			return 0.0;
		}
		return new BigDecimal(one).divide(new BigDecimal(two)).doubleValue();
	}
	/**
	 * 大数字相除，获取String类型
	 * @param one
	 * @param two
	 * @return
	 */
	public static String divide2(BigInteger one, BigInteger two){
		if(one == null || two == null || two.intValue() == 0){
			return "0.0";
		}
		return new BigDecimal(one).divide(new BigDecimal(two)).toString();
	}

	/**
	 * 两个long型数字相除
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static double divide(long one, long two) {
		return new BigDecimal(one+"").divide(new BigDecimal(two+"")).doubleValue();
	}


	/**
	 * 两个大整形数字相乘
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigInteger multiply(BigInteger one, BigInteger two) {
		return one.multiply(two);
	}
	
	/**
	 * 两个大数字相乘:返回BigInteger，去掉小数部分，
	 * 
	 * *注意：不进行四舍五入
	 * 
	 * 
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigInteger multiply(BigDecimal one, BigDecimal two) {
		return one.multiply(two).toBigInteger();
	}
	
	/**
	 * 两个大数字相乘:返回BigInteger，四舍五入,去掉小数
	 * 
	 * *注意：进行四舍五入
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigInteger multiply2(BigDecimal one, BigDecimal two) {
		BigDecimal value = one.multiply(two).setScale(0,RoundingMode.HALF_UP);
		return value.toBigInteger();
	}
	/**
	 * 两个long型数字相乘
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigInteger multiply(long one, long two) {
		return new BigInteger(one+"").multiply(new BigInteger(two+""));
	}
	
	/**
	 * 两个数字相乘
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigInteger multiply(double one, int two) {
		return new BigDecimal(one+"").multiply(new BigDecimal(two+"")).toBigInteger();
	}
	
	/**
	 * 两个double型数字相乘，获取大数字
	 * @param one
	 * @param two
	 * @return
	 */
	public static BigDecimal multiply(double one,double two){
		return new BigDecimal(one+"").multiply(new BigDecimal(two+""));
	}
}
